<?php
/**
 * Created by PhpStorm.
 * User: 64423
 * Date: 2018/10/24
 * Time: 9:27
 */

namespace app\api\home;


class Index extends Common
{
    public function banner()
    {
        $list = db('banner')->order('sort_order desc')->select();
        foreach ($list as $k=>$v){
            $list[$k]['img_url'] = 'http://'.$host = $_SERVER['HTTP_HOST'].get_file_path($v['img_url']);
        }

        $data = [
            'banner' => $list
        ];
        return _json(0, '执行成功',$data);
    }
    // 首页顶级分类导航
    public function channel()
    {
        $list = db('category')->where('show_index',1)->where('parent_id',0)
            ->limit(5)->order('sort_order desc')->select();
        $data = [
            'channel' => $list
        ];
        return _json(0, '执行成功',$data);
    }

    //首页 新品
    public function newGoods()
    {
        $list = db('goods')->where('is_sell',1)->where('is_new',1)
            ->order('sort_order desc')->select();
        foreach ($list as $k=>$v){
            $list[$k]['thumb_url'] = 'http://'.$host = $_SERVER['HTTP_HOST'].get_file_path($v['thumb_url']);
        }
        $data = [
            'newGoods' => $list
        ];
        return _json(0, '执行成功',$data);
    }

    // 人气推荐
    public function hotGoods()
    {
        $list = db('goods')->where('is_sell',1)->where('is_hot',1)
            ->order('sort_order desc')->select();
        foreach ($list as $k=>$v){
            $list[$k]['thumb_url'] = 'http://'.$host = $_SERVER['HTTP_HOST'].get_file_path($v['thumb_url']);
        }
        $data = [
            'hotGoods' => $list
        ];
        return _json(0, '执行成功',$data);
    }

    // 首页各分类商品展示
    public function category()
    {
        $category_list = db('category')->where('parent_id',0)->field('id,name')->order('sort_order desc')->select();
        foreach ($category_list as $k=>$v){

            $category_ids = [];
            array_push($category_ids, $v['id']);
            $child_ids = db('category')->where('parent_id',$v['id'])->column('id');

            $category_ids = array_merge($category_ids, $child_ids);

            $goods_list = db('goods')->whereIn('category_id',$category_ids)->where('is_sell',1)
                ->limit(20)->order('sort_order desc')->select();
            foreach ($goods_list as $k2=>$v2){
                $goods_list[$k2]['thumb_url'] = 'http://'.$host = $_SERVER['HTTP_HOST'].get_file_path($v2['thumb_url']);
            }
            $category_list[$k]['goodsList'] = $goods_list;
        }
        $data = [
            'categoryList' => $category_list
        ];
        return _json(0, '执行成功',$data);
    }

    // 搜索框商品数量
    public function count()
    {
        $count = db('category')->where('is_sell',1)->count();
        $data = [
            'count' => $count
        ];
        return _json(0, '执行成功',$data);
    }
}